package org.anderground.editor.edits;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;

import org.anderground.core.model.Station;
import org.anderground.core.util.Time;

public class SetScheduleAction extends Action {
	private Time openTime, closeTime;
	private Station station;

	public SetScheduleAction(Station station, Time openTime, Time closeTime) {
		this.openTime = openTime;
		this.closeTime = closeTime;
		this.station = station;
	}

	@Override
	UndoableEdit perform() throws ActionException {
		return new TimeEdit();
	}

	@SuppressWarnings("serial")
	private class TimeEdit extends AbstractUndoableEdit {

		public TimeEdit() {
			redo();
		}

		@Override
		public void undo() throws CannotUndoException {
			switchTimes();
		}

		@Override
		public void redo() throws CannotRedoException {
			switchTimes();
		}

		private void switchTimes() {
			switchOpenTimes();
			switchCloseTimes();

		}

		private void switchOpenTimes() {
			Time temp = openTime;
			openTime = station.getOpenTime();
			Editor.edit(station).setOpenTime(temp);
		}

		private void switchCloseTimes() {
			Time temp = closeTime;
			closeTime = station.getOpenTime();
			Editor.edit(station).setCloseTime(temp);
		}

	}

}
